package com.jxb.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.jxb.service.UserService;

/**
 * spring security 认证管理器
 *
 * @author jiaobo
 * @date Created in 2024/10/26 13:21
 **/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //自定义页面配置
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.formLogin()//表单提交
                .loginPage("/login.html")//自定义登录页面
                .loginProcessingUrl("/user/login")//登录访问路径，必须和表单提交接口一样
                .defaultSuccessUrl("/admin/index") //认证成功之后跳转的路径
                .and().authorizeRequests()
                .antMatchers("/user/login","/login.html").permitAll()
                .antMatchers("/admin/demo").access("hasAuthority('System')")
                .anyRequest().authenticated()//需要认证
                .and().csrf().disable();//关闭csrf防护
    }

    // @Override
    // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //     String encodePasswd = passwordEncoder().encode("123456");
    //     auth
    //         //使用基于内存的InMemoryUserDetailsManager
    //         .inMemoryAuthentication()
    //             //配置用户
    //         .withUser("bojiao3").password(encodePasswd).roles("admin")
    //             //配置其他用户
    //             .and()
    //             .withUser("zhangsan").password(encodePasswd).roles("user");
    // }

    //配置加密方式
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");
        System.out.println(encode);
    }
}
